package main

import (
	"github.com/shopspring/decimal"
)

type Rows struct {
	Rows []string
}

func (rows *Rows) Add(newRows []string) (err error) {
	max := len(rows.Rows)
	if len(newRows) > max {
		max = len(newRows)
	}
	sum := make([]string, max)
	for i := 0; i < max; i++ {
		old := decimal.NewFromInt(0)
		new := decimal.NewFromInt(0)
		errCount := 0
		empty := 0
		if len(newRows) > i {
			if newRows[i] != "" {
				new, err = decimal.NewFromString(newRows[i])
				if err != nil {
					errCount++
					sum[i] = newRows[i]
				}
			} else {
				empty++
			}

		} else {
			empty++
		}
		if len(rows.Rows) > i {
			if rows.Rows[i] != "" {
				old, err = decimal.NewFromString(rows.Rows[i])
				if err != nil {
					errCount++
					sum[i] = rows.Rows[i]
				}
			} else {
				empty++
			}

		} else {
			empty++
		}
		if errCount == 0 {
			if empty != 2 {
				sum[i] = old.Add(new).String()
			}

		}

	}
	rows.Rows = sum
	return nil
}
